#import "BWFabricInspectorPane.h"
#import "BWBorkStitchDocument.h"
#import "BWDocumentUndo.h"
#import "BWConfigTemplate.h"
#import "BWConfigPopUpButton.h"


@implementation BWFabricInspectorPane

- (id) init
{
    if (self = [super init]) {
        if (![NSBundle loadNibNamed: @"BWFabricInspectorPane"
                       owner: self]) {
            NSLog (@"could not load BWFabricInspectorPane.nib");
        }
    }

    return (self);

} // init


- (NSImage *) toolbarIcon
{
    NSImage *image;
    image = [NSImage imageNamed: @"fabric-pane.tiff"];

    return (image);

} // toolbarIcon


// --------------------------------------------------
// UI foobage

- (void) updateUI
{
    if (document == nil) {
        return;
    }

    [fabricWidthSlider setFloatValue: [[document grid] fabricWidth]];
    [fabricSpacingSlider setFloatValue: [[document grid] fabricSpacing]];
    
    [fabricBackgroundColorWell setColor: [[document grid] backgroundColor]];
    [fabricThreadColorWell setColor: [[document grid] threadColor]];
    [fabricEdgeColorWell setColor: [[document grid] edgeColor]];
    
    if ([[document grid] flatGrid]) {
        [fabricFlatGridCheckbox setState: NSOnState];
    } else {
        [fabricFlatGridCheckbox setState: NSOffState];
    }

} // updateUI


- (IBAction) fabricSetWidth: (id) sender
{
    float value = [sender floatValue];

    [document changeFabricWidth: value];

    [document setNeedsDisplay: YES];

} // fabricSetWidth


- (IBAction) fabricSetSpacing: (id) sender
{
    float value = [sender floatValue];
    [document changeFabricSpacing: value];
    [document setNeedsDisplay: YES];

} // fabricSetSpacing


- (IBAction) fabricSetBackgroundColor: (id) sender
{
    NSColor *color;
    color = [sender color];

    [document changeFabricBackgroundColor: color];
    [document setNeedsDisplay: YES];

} // fabricSetBackgroundColor


- (IBAction) fabricSetThreadColor: (id) sender
{
    NSColor *color;
    color = [sender color];

    [document changeFabricThreadColor: color];
    [document setNeedsDisplay: YES];

} // fabricSetThreadColor


- (IBAction) fabricSetEdgeColor: (id) sender
{
    NSColor *color;
    color = [sender color];

    [document changeFabricEdgeColor: color];
    [document setNeedsDisplay: YES];

} // fabricSetEdgeColor


- (IBAction) fabricSetFlatGrid: (id) sender
{
    int state = [sender state];

    [document changeFabricFlatGrid: (state == NSOnState)];

    [document setNeedsDisplay: YES];

} // fabricSetFlatGrid


- (NSString *) paneName
{
    return (@"BWFabricInspectorPane");
} // name




// --------------------------------------------------
// config pop up button delegate methods


- (NSString *) nameForConfigPopUpButton: (BWConfigPopUpButton *) button
{
    return (@"Fabric Templates...");

} // nameForConfigPopUpButton


- (NSString *) categoryNameForPopUpButton: (BWConfigPopUpButton *) button
{
    return (@"Fabric Templates");
} // categoryNameForPopu


- (NSWindow *) windowForConfigSheetForPopUpButton: (BWConfigPopUpButton *) button
{
    return ([paneView window]);

} // windowForConfigSheetForPopUpButtonAndBoytIsThisNameEntirelyTooLong


- (void) populateTemplate: (BWConfigTemplate *) template
           forPopUpButton: (BWConfigPopUpButton *) button
{
    [template setFloatValue: [[document grid] fabricWidth]
              forKey: @"Fabric Width"];
    [template setFloatValue: [[document grid] fabricSpacing]
              forKey: @"Fabric Spacing"];
    [template setBoolValue: [[document grid] flatGrid]
              forKey: @"Flat Grid"];
    
    [template setObjectValue: [[document grid] backgroundColor]
              forKey: @"Background Color"];
    [template setObjectValue: [[document grid] threadColor]
              forKey: @"Thread Color"];
    [template setObjectValue: [[document grid] edgeColor]
              forKey: @"Edge Color"];

} // populateTemplateForPopUpButton


- (void) useTemplate: (BWConfigTemplate *) template
     fromPopUpButton: (BWConfigPopUpButton *) button
{
    float width = [template floatValueForKey: @"Fabric Width"];
    float spacing = [template floatValueForKey: @"Fabric Spacing"];
    BOOL flat = [template boolValueForKey: @"Flat Grid"];
    NSColor *background, *thread, *edge;
    background = [template objectValueForKey: @"Background Color"];
    thread = [template objectValueForKey: @"Thread Color"];
    edge = [template objectValueForKey: @"Edge Color"];

    [document setFabricWidth: width
              spacing: spacing
              flat: flat
              background: background
              thread: thread
              edge: edge];
    
} // useTemplate


@end // BWFabricInspectorPane

